import requests
from lxml import etree

book_base_url = 'https://book.douban.com/top250?start={}'
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
}

# 将数据写入文件保存
with open("data/book_info.csv", mode="w", encoding='utf8') as f:
    for i in range(1, 11):
        book_url = book_base_url.format((i - 1) * 25)
        # 发起请求
        book_response = requests.get(url=book_url, headers=headers)
        print("当前正在处理:%s" % book_url)
        book_xpath = etree.HTML(book_response.text, etree.HTMLParser())
        tr_list = book_xpath.xpath("//tr[@class='item']")
        for tr in tr_list:
            # 提取每本书籍的信息
            name = tr.xpath(".//div[@class='pl2']/a/text()")[0].strip()
            book_infos = tr.xpath(".//p[@class='pl']/text()")[0].strip()
            book_infos_list = book_infos.split(" / ")
            # 通过逆向索引从右往左取值
            price = book_infos_list[-1].strip()
            date = book_infos_list[-2].strip()
            publish = book_infos_list[-3].strip()
            # 通过切片提取所有的作者
            authors_list = book_infos_list[-4::-1]
            # 通过字符串的join方法可以将list转成字符串
            authors = " | ".join(authors_list)

            rating_nums = tr.xpath(".//span[@class='rating_nums']/text()")[0]
            quote_span = tr.xpath(".//p[@class='quote']/span/text()")
            quote = ''
            if quote_span:
                quote = quote_span[0]
            f.write(
                name + "," + price + "," + date + "," + publish + "," + authors + "," + rating_nums + "," + quote + "\n")
